package com.zhhd.libdb;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

@Database(entities = {PatrolRecord.class}, version = 1, exportSchema = false)
public abstract class ProjectDatabase extends RoomDatabase {
    private static final String DB_NAME = "nfc_database.db";
    public abstract PatrolRecordDao getPatrolRecordDao();
    private static ProjectDatabase INSTANCE;
    private static final Object lock = new Object();

    public static ProjectDatabase getInstance(Context context){
        synchronized (lock){
            if (INSTANCE == null){
                INSTANCE = Room.
                        databaseBuilder(context.getApplicationContext(), ProjectDatabase.class , DB_NAME).
//                        addMigrations(MIGRATION_1_2).
                        build();
            }
            return INSTANCE;
        }
    }


    /**
     * 版本1-2的迁移策略
     * 构造方法需传 开始版本号 与 截止版本号
     */
    static final Migration MIGRATION_1_2 = new Migration(1,2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //将数据表device创建出来
            database.execSQL("CREATE TABLE 'location' ('id'  LONG,'latitude' DOUBLE,'longitude' DOUBLE,'accuracy' FLOAT,'altitude' FLOAT,'time' LONG,PRIMARY KEY ('id')) ");
        }
    };

}
